#!/usr/bin/perl
#printer-lib.pl
#Library for share printers

do "../web-lib.pl";
&init_config();

#############################################
###Those are for new checking and creating###

#remove the parport mod;
sub rm_parportmod()
{
	my $result;
	$result = system("/sbin/rmmod lp");
	if ($result = -1){
		system("/bin/mv /etc/printcap /etc/printcap.nas");
		system("/sbin/rmmod lp");
		system("/bin/mv /etc/printcap.nas /etc/printcap");
	}
	
	system("/sbin/rmmod parport_pc");
	system("/sbin/rmmod parport");		
}

#install the parportmod;
sub ins_parportmod()
{
	sleep (1);
	system("/sbin/modprobe parport_pc");
	system("/sbin/modprobe lp");
	system("$config{'lpd'} restart");
}

#get the parport description;
#param:file id
#the file is named: /etc/nas/webmin/printer/parid.lpr
#file form is:
#	dev=par
#	des="descripton"
sub get_par_des()
{
	open(FILE,"$config{'autoprobe'}")||return;
	@parconf=<FILE>;
	close(FILE);
	
	$result[0]="dev=par\n";
	$result[1]="des=\n";
	if (@parconf<2){
		return;
	}
	foreach (@parconf){
		if(/DESCRIPTION/){
			chomp($_);
			$_ =~ s/DESCRIPTION://;
			$_ =~ s/;.*$//;
			$result[1]="des=$_\n";
			last;			
		}
	}
	
	$filename = "$config{'configdir'}"."par"."$_[0].lpr";
	open(FILE,">$filename")||die;
	print FILE @result;
	close(FILE);
	
	$log_ev = 41001;
	$log_msg = 'find a printer with parallel port';
	&nas_syslog('check_printers', 'naslog_info', $log_ev, $log_msg);
}

#get usb printport infomation;
#param:file id;
#it will make 1~4 files ,and the id is increased by add 1;
#the file is named: /etc/nas/webmin/printer/usbid.lpr
#file form is:
#	bus=
#	port=***
#	dev=***
#but this file is not the last file;
#it will be changed by the other subfunction;
sub get_usb_info()
{
	open(FILE,"$config{'devices'}");
	@usbconf=<FILE>;
	close(FILE);
	
	$idnum=$_[0];
	$idcount=0;
	$i=0;
	$j=0;
	while ($i < @usbconf){
		if($usbconf[$i]=~/Driver=usblp/){
			$j=$i;
			while ($j>=0){
				$line=$usbconf[$j];
				$j=$j-1;
				if ($line=~/^T:/){
					chomp($line);
					$file[0]=$line;
					$file[0]=~ s/.*Bus=//;
					$file[0]=~ s/ .*//;
					$file[0]="bus=@file[0]\n";
					
					$file[1]=$line;
					$file[1]=~ s/.*Port=//;
					$file[1]=~ s/ .*//;
					$file[1]="port=@file[1]\n";
					
					$file[2]=$line;
					$file[2]=~ s/.*Dev\#=//;
					$file[2]=~ s/^ /0/;
					$file[2]=~ s/^0 /00/;
					$file[2]=~ s/ .*//;
					$file[2]="dev=$file[2]\n";
					
					$filename = "$config{'configdir'}"."usb"."$idnum.lpr";
					open(FILE,">$filename")||die;
					print FILE @file;
					close(FILE);					
					$idnum++;
					$idcount++;	
					last;
				}
			}
			$log_ev = 41002;
			$log_msg = 'find a printer with usb port';
			&nas_syslog('check_printers', 'naslog_info', $log_ev, $log_msg);
		}
		$i++;
	}		
}

#get usb description
#param is file name
#it will change the given file usb*.lpr
#file form will be changed as below:
#	bus=
#	port=***
#	dev=***
#	des=*******
sub get_usb_des()
{	
	my $filename;
	my @usbconfbk;
	my @usbconf;
	my $blen;
	my $file;
	my $command;
	my @tmpusb;
	my @parts;
	my $i;
	my $limit=$config{'usb-trylimit'};

	$filename = "$config{'configdir'}"."$_[0]";
	open(FILE,"$filename")||die;
	@usbconfbk=<FILE>;
	@usbconf=@usbconfbk;
	close(FILE);
	
	chomp($usbconf[0]);
	chomp($usbconf[2]);
	$usbconf[0]=~ s/\D+//;
	$usbconf[2]=~ s/\D+//;
	$blen=do{use bytes; length $usbconf[0]};
	if($blen == 2){
		$usbconf[0]="0".$usbconf[0];
	}	
	
	$file = "$config{'usbdir'}"."$usbconf[0]/".$usbconf[2];
	
	for($j=0;$j<$limit;$j++){				
		$command = $config{'usb-printer-id'};
		$command = "$command $file >$config{'tmpusb'} 2>/dev/null ";
		system("$command");
		open(FILE,"$config{'tmpusb'}")||die;
		@tmpusb=<FILE>;
		close(FILE);
		$command = "/bin/rm ".$config{'tmpusb'};
		system("$command");
	
		for($i=0;$i<@tmpusb;$i++){
			chomp($tmpusb[$i]);
			@parts=split(/;/,$tmpusb[$i]);
			foreach (@parts){
				if (/DES:/ || /DESCRIPTION:/ ){		
					chomp($_);
					$_ =~ s/^DES.*://;
					$usbconfbk[3]="des=$_\n";
					open(FILE,">$filename")||die;
					print FILE @usbconfbk;
					close(FILE);
					return;
				}
			}
		}
	}
	
	$usbconfbk[3]="des=unknow\n";
	open(FILE,">$filename")||die;
	print FILE @usbconfbk;
	close(FILE);
}


#get usb printer description
#it will change the whole usb*.lpr files
#add des=*******
sub get_usb_desfile()
{
	system("/sbin/rmmod printer");
	
	opendir(DIR, $config{'configdir'});
	foreach $m (readdir(DIR)) {
		if ($m =~ /^usb/){
			&get_usb_des($m);
		}
	}
	closedir(DIR);	
	
	sleep(1);
	system("/sbin/modprobe printer");
}


#get usb description
#param is file name
#it will change the given file usb*.lpr
#file form will be changed as below:
#	bus=
#	port=***
#	dev=***
#	des=*******
#	lp=lp*
sub deal_dmesg()
{
	$filename = "$config{'configdir'}"."$_[0]";
	open(FILE,"$filename")||die;
	@usbconfbk=<FILE>;
	@usbconf=@usbconfbk;
	close(FILE);
	
	chomp($usbconf[2]);
	$usbconf[2]=~ s/\D+//;
	$usbconf[2]=~ s/^0*//;
	
	open(FILE,"$config{tmpdmesg}");
	@tmpfile=<FILE>;
	close(FILE);	
	
	$i=@tmpfile;
	$restul="";
	while($i>0){
		$line=$tmpfile[$i];
		chomp($line);
		if ($line =~ /^printer.c:/){
			if($line =~ /dev/){
				$line =~ s/.*dev //;
				$line =~ s/\D.*//;

				if ($usbconf[2] eq $line){
					$line=$tmpfile[$i];
					chomp($line);
					$line =~ /^printer.c: usb(\S+)/;
					$line =$1;
					$line =~ s/://;					
					$result =$line;
					last;
				}
			}
		}
		
		$i=$i-1;
	}
	
	
	$usbconfbk[4]="lp=$result\n";

	open(FILE,">$filename")||die;
	print FILE @usbconfbk;
	close(FILE);
}

#get usb lp count
#it will change the whole usb*.lpr files
#add lp=lp*
sub get_dmesg()
{
	system("/bin/dmesg > $config{'tmpdmesg'}");
	
	opendir(DIR, $config{'configdir'});
	foreach $m (readdir(DIR)) {
		if ($m =~ /^usb/){
			&deal_dmesg($m);
		}
	}
	closedir(DIR);	
	
	system("/bin/rm $config{'tmpdmesg'}");
}


sub rm_lprfile()
{
	$filename1 =$config{'configdir'}."*.lpr";
	$filename2 =$config{'configdir'}."*.valid";
	system("/bin/rm -f $filename1");
	system("/bin/rm -f $filename2");
}


##########
#check all the printers
#and create the files
##########
sub check_printers()
{
	&rm_lprfile;
	
	#get par port info and make par1.lpr file
	&rm_parportmod;
	&ins_parportmod;
	&get_par_des(1);


	#get usb port info and make usb1.lpr file
	&get_usb_info(1);
	&get_usb_desfile;
	&get_dmesg;	
}



#get printer number in the printcap
#return: printnum
sub get_printnum()
{
	my @printnum;
	open(FILE,$config{'printcap'})||return (@printnum);
	@printcap=<FILE>;
	close(FILE);
	
	$i=0;
	$printnum=0;
	while ($i<@printcap){
		$line=$printcap[$i];
		if ($line =~ /^printer/){
			chomp($line);
			$line =~ /^printer(\d+)|.*/;
			push(@printnum,$1);	
		}
		$i++;
	}
	
	return(@printnum);	
}

#get a new printer number 
#param: \@printnum
sub get_num()
{
	$number=$_[0];
	$count=@{$number};
	
	$num=1;
	while ($num<=$count){
		for($tmp=0;$tmp<$count; ){
			if($num == ${$number}[$tmp]){
				last;
			}
			$tmp++;
			if($tmp == $count){
				return ($num);
			}
		}
		$num++;
	}

	$count++;
	return ($count);
}

#check if the printcap have this printer;
#param: description, lp;
#return: 0-have,1-not have;
sub get_printcap()
{
	open(FILE,$config{'printcap'})||return ('no');
	@printcap=<FILE>;
	close(FILE);
	
	$j=0;
	while ($j<@printcap){
		$line=$printcap[$j];
		if ($line =~ /\|$_[0]:/){
			$line =~ /printer(\d+).*/;
			$ret = "printer".$1;
			$j=$j+6;
			$line=$printcap[$j];
			chomp($line);
			$line =~ s/.*:lp=//;
			$line =~ s/:.*//;
			if($line eq $_[1]){
				return ($ret);
			}		
		}
		$j++;
	}
	
	return ('no');
	
}

#add a new printer
#param: description, lp, printnumber
sub set_printcap()
{
	open(FILE,$config{'printcap'});
	@printcap=<FILE>;
	close(FILE);
	
	$printname="printer"."$_[2]";
	$i=@printcap;
	$printcap[$i]="\n$printname"."|$_[0]:\\\n";
	$printcap[$i+1]="	:ml=0:\\\n";
	$printcap[$i+2]="	:mx=0:\\\n";
	$printcap[$i+3]="	:sd=/var/spool/lpd/$printname:\\\n";
	$printcap[$i+4]="	:af=/var/spool/lpd/$printname/$printname".".acct:\\\n";
	$printcap[$i+5]="	:sh:\\\n";
	$printcap[$i+6]="	:lp="."$_[1]\n";
	
	open(FILE,">$config{'printcap'}")||die;
	print FILE @printcap;
	close(FILE);
	
}

#add
#param: printer number;
#sub add_printfile()
#{
#	$dirname= "$config{'varlpdir'}"."printer".$_[0];
#	$filename = $diename."printer".$_[0]."acct";
#	
#	print "$dirname	\n";
#	print "$filename	\n";
#
#	system("/bin/mkdir $dirname");
#	system("/bin/chmod 700 $dirname");
#	
#	system("/bin/touch $filename");
#	system("/bin/chmod 600 $filename");	
#}

##########
#check all the printer files
#and create new printers
##########
sub check_printcap()
{
	my @num;
	my @validlp;
	@num=&get_printnum;
	
	$i=0;
	opendir(DIR, $config{'configdir'});
	foreach $m (readdir(DIR)) {
		if ($m =~ /lpr$/){
			$filename = "$config{'configdir'}"."$m";
			open(FILE,"$filename")||die;
			@conf=<FILE>;
			close(FILE);
	
			if ($m=~ /^usb/){
				chomp($conf[3]);
				chomp($conf[4]);
				$descript=$conf[3];
				$descript=~ s/^des=//;
				$lp=$conf[4];
				$lp=~ s/^lp=//;
				$lp="/dev/usb/".$lp;
			}elsif($m=~ /^par/){
				chomp($conf[1]);
				$descript=$conf[1];
				$descript=~ s/^des=//;
				$lp= "/dev/lp0";	
			}
			
			$result=&get_printcap($descript,$lp);
			#print "result is $result\n";
			if ($result eq 'no'){
				$number=&get_num(\@num);
				&set_printcap($descript,$lp,$number);
				push (@num,$number);
				$result="printer".$number;
			}
			
			$validlp[$i]=$result."=".$m."\n";
			$i++;
		}
	}
	closedir(DIR);	
	
	open(FILE,">$config{'lpvalid'}")||die;	
	print FILE @validlp;
	close(FILE);
	
	system("$config{'lpd'} restart");
}


#############################################
#####Those are for getting informations######

#check all printers' name in smb.conf
sub get_smb_lpr()
{
	my @printname1;
	
	open(FILE,$config{'smb'})||return @printname1;
	@file=<FILE>;
	close(FILE);
	
	foreach $line (@file){
		if ($line =~ /printer =/){
			chomp($line);
			$line =~ /.*printer = printer(\d+).*/;
			$name = "printer".$1;
			push (@printname1,$name);
		}
	}
	
	return @printname1;
}

#check all printers' name in printcap
sub get_princap_lpr()
{
	my @printname2;
	my $i;
	my $line;
	my $des;
	my $mod;
	
	open(FILE,$config{'printcap'})||return @printname2;
	@file=<FILE>;
	close(FILE);
	
	$i=0;
	while ($i<@file){
		$line = $file[$i];
		if ($line =~ /^printer/){
			chomp($line);
			$line =~ /^printer(\d+).*/;
			$name = "printer".$1;
			$line =~ s/.*\|//;
			$line =~ s/:\\.*//;
			$des=$line;
			$i=$i+6;
			$line =$file[$i];
			$mod='par';
			if ($line =~ /usb/){
				$mod='usb';
			}
			push @printname2,{name=>$name,des=>$des,mod=>$mod};
		}
		$i++;
	}
	
	return @printname2;	
}

#check all valid printers' name in printer.valid
sub get_valid_lpr()
{
	my @printname3;
	
	open(FILE,$config{'lpvalid'})||return @printname3;
	@file=<FILE>;
	close(FILE);
	
	foreach $line (@file){
		if ($line =~ /^printer/){
			chomp($line);
			$line =~ /^printer(\d+).*/;
			$name = "printer".$1;
			push (@printname3,$name);
		}
	}
	
	return @printname3;		
}

#get shared printer's name
#param: printer name
#return: shared name
sub get_lpsmbname()
{
	my $name="";
	my $i;
	my $line;
	
	open(FILE,$config{'smb'})||die;
	@file=<FILE>;
	close(FILE);
	
	$i=0;
	while ($i<@file){
		$line =$file[$i];
		if ($line =~ /printer = $_[0]/){
			$i--;
			for($line = $file[$i]; $line !~ /^\[/ ;$i--){
				$line = $file[$i];
			}
			chomp ($line);			
			$line =~ s/^\[//;
			$line =~ s/\].*//;
			$name=$line;
			last;
		}
		$i++;
	}
	
	return $name;	
}

#get printer's name
#param: printer name
#return: printer mod
sub get_lpmod()
{
	my $mod='';
	my $i;
	my $line;
	
	open(FILE,$config{'printcap'})||return;
	@file=<FILE>;
	close(FILE);
	
	$i=0;
	while ($i<@file){
		$line =$file[$i];		
		if ($line =~ /^$_[0]\|/){
			chomp ($line);
			$line =~ s/^$_[0]\|//;
			$line =~ s/:\\//;
			$mod=$line;
			last;
		}
		$i++;
	}
	
	return $mod;	
}


#get the usb printer port
#param: printer name
#return: @info{'bus','port'}
sub get_lpinfo()
{
	my @file1;
	my @file2;
	my @info;
	my @infosub;
	
	push(@infosub,'no');
	push(@infosub,'no');
	
	open(FILE,$config{'lpvalid'})||return @infosub;
	@file1 = <FILE>;
	close(FILE);
	
	foreach(@file1){
		if (/^$_[0]/){
			chomp($_);
			s/.*=//;
			$lprfile=$_;
			last;
		}
	}
	
	open(FILE,$config{'configdir'}."$lprfile")||return @infosub;
	@file2 = <FILE>;
	close(FILE);
	
	foreach(@file2){
		if (/^bus=/){
			chomp($_);
			s/.*=//;
			$bus=$_;
			push(@info,$bus);
		}
		if(/^port=/){
			chomp($_);
			s/.*=//;
			$port=$_;
			push(@info,$port);
			return (@info);
		}
	}
	
	push(@info,'no');
	push(@info,'no');
	return (@info);
	
}

##########
#check all the printers in three files
#and return a hash 
##########
sub check_printstat()
{
	my @smblp;
	my @caplp;
	my @validlp;
	my %printers;
	my $capnum;
	my $i;
	my $name;
	my $des;
	my $mod;
	
	@smblp=&get_smb_lpr;
	@caplp=&get_princap_lpr;
	@validlp=&get_valid_lpr;

	for $capnum (@caplp){
		$name=$capnum->{'name'};
		$des=$capnum->{'des'};
		$mod=$capnum->{'mod'};
		$printers{$name}={smb => "no",valid => "no",des=>$des,mod=>$mod};
	}
		
	foreach(@smblp){
		$printers{"$_"}{'smb'}="yes";	
	}

	foreach(@validlp){
		$printers{"$_"}{'valid'}="yes";	
	}
	
	return %printers;
}


#change port to place
#param: lpport, lpbus
#return: place
sub get_lpplace()
{
	if($_[0] eq 'no'){
		return $text{'nousb'};
	}else{
		if (($_[0] eq '00')&&($_[1] eq '01')){
			return $text{'place1'};
		}elsif(($_[0] eq '00')&&($_[1] eq '02')){
			return $text{'place2'};
		}elsif(($_[0] eq '01')&&($_[1] eq '01')){
			return $text{'place3'};
		}elsif(($_[0] eq '01')&&($_[1] eq '02')){
			return $text{'place4'};
		}else{
			return "";
		}
	}	
	
}

#print all the printers in index.cgi
sub printers()
{
	%printers=&check_printstat;
	
	@printkey = keys (%printers);
	
	$mode='mode2';
	foreach (@printkey){
		$lpname=$_;
		$lpsmb=$printers{$_}{'smb'};
		$lpvalid=$printers{$_}{'valid'};
		$lpdes=$printers{$_}{'des'};
		$lpmod=$printers{$_}{'mod'};
		if ($lpsmb eq 'yes'){
			$lpsmbname=&get_lpsmbname($lpname);
		}else{
			$lpsmbname="";
		}
		if (($lpmod eq 'usb')&&($lpvalid eq 'yes')){
			@lpinfo=&get_lpinfo($lpname);
			$lpport=pop(@lpinfo);
			$lpbus=pop(@lpinfo);
		}else{
			$lpport="no";
			$lpbus="no";
		}
		
		if ($mode eq 'mode1'){
			$mode='mode2';
		}else{
			$mode='mode1';
		}

		if ($lpvalid eq 'yes'){
			$lpplace=&get_lpplace($lpport,$lpbus);
			printf "<tr class=%s> <td><input type=radio name=printer value=%s>[ $lpdes ]</td>\n",$mode,$lpname;
			printf "<td class=%s>$lpplace</td>\n",$mode;
		}else{
			printf "<tr class=%s> <td><input type=radio name=printer value=%s><strike>[ $lpdes ]</strike></td>\n",$mode,$lpname;
			printf "<td class=%s></td>\n",$mode;
		}
	
		if ($lpsmb eq 'yes'){
			printf "<td class=%s>$lpsmbname</td>\n",$mode;
		}else{
			printf "<td class=%s><i>$text{'nosmb'}</i></td>\n",$mode;
		}
		print "</tr>\n";
	}
}

#check if the printer is valid
#param: printer name
sub check_valid()
{
	open(FILE,$config{'lpvalid'})||return "no";
	@file=<FILE>;
	close(FILE);
	
	foreach $line (@file){
		if ($line =~ /^printer/){
			chomp($line);
			$line =~ /^printer(\d+).*/;
			$name = "printer".$1;
			if ($name eq $_[0]){
				return "yes";
			}
		}
	}	

	return "no";
}

#delete the printer in smb
#param: printer name
sub del_smbprinter()
{
	my @file1;
	my @file2;
	my $i;
	my $have;
	my $j;
	my $count;
	
	open(FILE,$config{'smb'})||die;
	@file1=<FILE>;
	close(FILE);
	
	for($i=0;$i<@file1;$i++){
		$line=$file1[$i];
		if ($line =~ /^\[/){
			$j = $i+1;
			$count = 1;
			$line = $file1[$j];
			$have = 'no';
			while (($line !~ /^\[/) && ($j<@file1)){
				chomp($line);
				if ($line =~ /printer = $_[0]$/){
					$have = 'yes';	
				}
				$j++;
				$count++;
				$line =$file1[$j];
			}
			
			if ($have eq 'no'){				
				for ($j =0;$j<$count;$j++){					
					push(@file2,$file1[$i+$j]);
				}
			}
			
			$i=$i+$count-1;					
		}else{
			push(@file2,$line);
		}
	}

	open(FILE,">$config{'smb'}")||die;
	print FILE @file2;
	close(FILE);

	system("/etc/init.d/smb restart");
}

#delete the printer in printcap
#param: printer name
sub del_capprinter()
{
	my @file1;
	my @file2;
	my $i;
	my $have;
	my $j;
	my $count;
	my $dirname;
	
	@file1=undef;
	@file2=undef;
	open(FILE,$config{'printcap'})||return;
	@file1=<FILE>;
	close(FILE);
	
	for($i=0;$i<@file1;$i++){
		$line=$file1[$i];
		if ($line =~ /^$_[0]\|.*/){
			$j = $i+1;
			$count = 1;
			$line = $file1[$j];
			while (($line !~ /^printer/) && ($j<@file1)){
				$j++;
				$count++;
				$line = $file1[$j];
			}
			$i=$i+$count-1;	
		}else{
			push(@file2,$line);
		}
	}

	open(FILE,">$config{'printcap'}")||die;
	print FILE @file2;
	close(FILE);

	$dirname = $config{'varlpdir'}."$_[0]";
	system("/bin/rm -Rf $dirname");

	system("$config{'lpd'} restart");
}

#config the printer's sharing
#param: printer name
sub get_smbinfo()
{
	my @lpinfo;
	my @file;
	my $i;
	
	open(FILE,$config{'smb'})||die;
	@file=<FILE>;
	close(FILE);	
	for ($i=0;$i<@file;$i++){
		$line = $file[$i];
		if($line =~ /^\[$_[0]\]/){
			$i++;
			$line = $file[$i];
			while (($line !~ /^\[/) && ($i<@file)){
				chomp($line);	
				if ($line =~/printer admin =/){
					$line =~ s/.*printer admin = //;					
					push @lpinfo, "-";
					@member=split(/,/,$line);
					foreach (@member){
						push @lpinfo,$_;
					}
				}elsif($line =~ /valid users =/){
					$line =~ s/.*valid users = //;
					push @lpinfo, "--";					
					@member=split(/,/,$line);					
					foreach (@member){
						push @lpinfo,$_;
					}
				}elsif($line =~ /public =/){
					$line =~ /.*public = (\w+)/;
					push @lpinfo,$1;				
				}
				$i++;
				$line = $file[$i];
			}
		}	
	}

	return @lpinfo;
}

#check if the shared name is duplicated
#param: new smb name
sub check_smbname()
{
	my $result='';
	my @smbconf;

	open(FILE,$config{smb})||die;
	@smbconf = <FILE>;
	close(FILE);
	
	foreach(@smbconf){
		if (/^\[$_[0]\]/){
			$result = 'same';
			last;
		}
	}
	
	if ($result ne 'same'){
		if ($_[0] =~ /[^A-Za-z0-9\x80-\xff\_\-\(\)]/){			
			$result = 'wrong';
		}elsif(length($_[0]) > 23){
			$result = 'wrong';
		}
	}
	return $result;
	
}

#not shared printer
#param: shared name
sub del_smblp()
{
	my @smbconf;
	my @smbconfsub;
	my $find='no';
	my $smboldname;
	
	open(FILE,$config{smb})||die;
	@smbconf = <FILE>;
	close(FILE);
	
	$smboldname = $_[0];
	for($i=0;$i<@smbconf;$i++){
		$line = $smbconf[$i];
		if($line =~ /^\[$_[0]\]/){
			$i++;
			$line = $smbconf[$i];
			while (($line !~ /^\[/) && ($i<@smbconf)){
				$i++;
				$line = $smbconf[$i];
			}
		}
		push (@smbconfsub,$smbconf[$i]);	
	}	
	
	open(FILE,">$config{smb}")||die;
	print FILE @smbconfsub;
	close(FILE);	
	
	system("/etc/init.d/smb restart");
}


#add a new shared printer in the smb.conf
#param: shared name, printer name,public,users,admins;
sub set_newsmblp()
{
	my @printer;
	my $public;
	my @users;
	my @admins;
	my $users;
	my $admins;
	
	#@users=split(/:/,$_[3]);
	@admins=split(/:/,$_[4]);
	
	$users=join(",",@users);
	$admins=join(",",@admins);

	#if($_[2] eq "yes"){
		$public="yes";
	#	$users="";		
	#}else{
	#	$public="no";
	#}
	
	push @printer, "[$_[0]]\n";
	#push @printer, "	valid users = $users\n";
	push @printer, "	valid users = \n";
	push @printer, "	writable = no\n";
	push @printer, "	path = /tmp\n";
	push @printer, "	printer = $_[1]\n";
	push @printer, "	printable = yes\n";
	push @printer, "	postscript = no\n";
	push @printer, "	browseable = yes\n";
	push @printer, "	printer admin = $admins\n";
	push @printer, "	public = $public\n";

	open(FILE,">>$config{smb}")||die;
	print FILE @printer;
	close(FILE);
	
	system("/etc/init.d/smb restart");
}

#change the shared printer
#param: oldsmbname,newsmbname,public,users,admins;
sub save_smblp()
{
	my @smbconf;
	my @smbconfsub;
	my $find='no';
	my @printer;
	my @users;
	my @admins;
	my $users;
	my $admins;
	my $public;
		
	@users=split(/:/,$_[3]);
	@admins=split(/:/,$_[4]);
	
	$users=join(",",@users);
	$admins=join(",",@admins);
	
	if($_[2] eq "yes"){
		$public="yes";
		$users="";
	}else{
		$public="no";
	}

	open(FILE,$config{'smb'})||die;
	@smbconf = <FILE>;
	close(FILE);
	
	$i=0;
	for($i=0;$i<@smbconf;$i++){
		$line = $smbconf[$i];
		if ($line =~ /^\[$_[0]\]/){
			$fine = 'yes';
			push (@smbconfsub,"[$_[1]]\n");
			next;
		}
		if ($fine eq 'yes'){
			#if ($line =~ /public =/){
			#	push (@smbconfsub,"	public = $public\n");
			#	next;				
			#}elsif($line =~ /valid users =/){
			#	push (@smbconfsub,"	valid users = $users\n");
			#	next;			
			#}elsif($line =~ /printer admin =/){
			if($line =~ /printer admin =/){
				push (@smbconfsub,"	printer admin = $admins\n");
				next;							
			}elsif($line=~/^\[/){
				$fine = 'no';
			}
		}
		push (@smbconfsub,$smbconf[$i]);	
	}	

	open(FILE,">$config{smb}")||die;
	print FILE @smbconfsub;
	close(FILE);

	system("/etc/init.d/smb restart");
}


